package com.yx.gsyd.controller.app;


import com.github.liujiebang.pay.wx.entity.WxOAuth2Info;
import com.github.liujiebang.pay.wx.entity.WxUserInfo;
import com.github.liujiebang.pay.wx.service.WxAuthService;
import com.yx.gsyd.comm.ResultEnum;
import com.yx.gsyd.entity.WxUser;
import com.yx.gsyd.exception.MyException;
import com.yx.gsyd.satoken.StpUserUtil;
import com.yx.gsyd.service.IWxUserService;
import com.yx.gsyd.util.ResultView;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2024-06-29
 */
@RestController
@RequestMapping("/api/front/wx-user")
public class AppWxUserController {

    @Autowired
    private WxAuthService wxAuthService;

    @Autowired
    private IWxUserService iWxUserService;

    /**
     * 公众号登录回调
     * @return
     */
    @GetMapping("/wxAuthaccessToken")
    @ResponseBody
    public ResultView wxAuthaccessToken(HttpServletRequest request) {
        String code=request.getParameter("code");
        if(StringUtils.isEmpty(code)){
            throw new MyException(ResultEnum.CODE_401,"微信授权code为null异常");
        }
        WxOAuth2Info wxOAuth2Info = null;
        try {
            wxOAuth2Info = wxAuthService.wxPpOAuth2AccessToken(code);
        } catch (Exception e) {
            e.printStackTrace();
            throw new MyException(ResultEnum.CODE_401,e.getMessage());
        }
        if(wxOAuth2Info==null){
            throw new MyException(ResultEnum.CODE_401,"微信授权token异常");
        }
        String openId = wxOAuth2Info.getOpenid();
        if(StringUtils.isEmpty(openId))
            throw new MyException(ResultEnum.CODE_401,"微信授权OPEID获取异常");{
        }
        //根据code获取用户详细信息数据
        WxUserInfo wxUserInfo=null;
        try {
            wxUserInfo=wxAuthService.wxOAuth2getUserInfo(wxOAuth2Info.getAccess_token(),wxOAuth2Info.getOpenid());
        } catch (Exception e) {
            e.printStackTrace();
            throw new MyException(ResultEnum.CODE_401,e.getMessage());
        }
        if(wxUserInfo==null){
            throw new MyException(ResultEnum.CODE_401,"微信授权获取用户详细信息异常");
        }
        //进行数据处理
        WxUser wxUser=iWxUserService.saveOrUpdateWxUser(wxUserInfo);
        if(wxUser==null){
            throw new MyException(ResultEnum.CODE_401,"微信授权获取用户详细保存异常");
        }
        //进行授权注入sa-token权限框架
        StpUserUtil.login(wxUser.getUserId());
        //注入用户session-保持session信息共享-保持session底层代码共享
        return ResultView.ok(StpUserUtil.getTokenValue());
    }


    /**
     * 模拟生成微信授权后token
     */
    @GetMapping(value = "/createToken")
    public ResultView createToken() {
        WxUser wxUser=this.iWxUserService.getById(1);
        //进行授权注入sa-token权限框架
        StpUserUtil.login(wxUser.getUserId());
        return ResultView.ok(StpUserUtil.getTokenValue());
    }


    /**
     * 模拟生成微信授权后token
     */
    @GetMapping(value = "/getAccountInfo")
    public ResultView getAccountInfo() {
        WxUser wxUser=this.iWxUserService.getById(StpUserUtil.getLoginIdAsInt());
        return ResultView.ok(wxUser);
    }


}
